package com.xingzhe.pengji.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


//@EnableWebMvc
@Configuration
public class SpringBeanMvc extends WebMvcConfigurerAdapter
{
    //对模型视图名称的解析，即在模型视图名称添加前后缀 
    //@EnableWebMvc注解和@Configuration注解一起使用启用了默认的Spring MVC配置，它等于<mvc:annotation-driven />。使用@ComponentScan注解，我们可以确保我们的@Controller将被添加到应用上下文。该配置类同时也定义了@Bean作为我们默认的视图获取器。
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
    
    //设置默认的系统好了编码格式
    @Bean
    public CommonsMultipartResolver configureCommonsMultipartResolver()
    {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("utf-8");
        return resolver;
    }
    
    //	 启动Spring MVC的注解功能，完成请求和注解POJO的映射
    //	 配置一个基于注解的定制的WebBindingInitializer，解决日期转换问题，
    //	方法级别的处理器映射， 有人说该bean要放在context:component-scan前面，要不然不起作用，但我试的放后面也可以啊。
//    @Bean
//    public AnnotationMethodHandlerAdapter configureAnnotationMethodHandlerAdapter()
//    {
//        AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
//        adapter.setCacheSeconds(0);
//        adapter.setWebBindingInitializer(new MyWebBinding());
//        return adapter;
//    }
//    
    //org.springframework.web.multipart.commons.CommonsMultipartResolver
    //	<!-- <ajax+springmvc异步上传> -->
    @Bean
    public CommonsMultipartResolver configuCommonsMultipartResolver()
    {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(10485760);
        return resolver;
    }
    
    public DefaultAnnotationHandlerMapping configDefaultAnnotationHandlerMapping()
    {
        DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = new DefaultAnnotationHandlerMapping();
        return defaultAnnotationHandlerMapping;
    }
    
    public AnnotationMethodHandlerAdapter configAnnotationMethodHandlerAdapter()
    {
        AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter();
        return annotationMethodHandlerAdapter;
    }
    
}
